"=====================待解决问题======================================================
"@@@vim配置文件是编程,随意调整行顺序可能导致错误!!
"1.忘记什么是块选按键组合
"2.光标移动到上次编辑的位置
"3.开启相对行号
"=====================待解决问题======================================================
"------------------------------------------------------------------------------------------------------

"=====================self-@@高频设定+通用设定或按键组合 ======================================================

"self:<待定> 在cmd和insert模式下Alt+r 用作 Redo,因为pycharm的ctrl+R用于搜索快捷键
inoremap <M-r> <esc><c-r>i
nnoremap <M-r> <c-r>

""self:因在Alt多被系统占用以及Vsvim中未实现Alt键的诸多功能，暂停使用Alt+H退出插入模式
"imap <M-k> <ESC>

" 用空格键来开关折叠
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>

" 启用每行超过80列的字符提示（字体变蓝并加下划线）,注掉可关闭此功能
au BufWinEnter * let w:m2=matchadd('Underlined', '\%>' . 80 . 'v.\+', -1)

set nocompatible                                      "禁用 Vi 兼容模式
syn on						      "语法支持

"下面功能Vs和Pycharm的两个vim插件都不支持,gvim支持
"设置相对行号,normal模式首次打开文件显示绝对行号，以后显示相对行号
"insert模式显示绝对行号
set nu
augroup relative_numbser
 autocmd!
 autocmd InsertEnter * :set norelativenumber
 autocmd InsertLeave * :set relativenumber
augroup END

"==============以下为前置变量设定，不得后移=============

" =============================================================================
"        << 判断操作系统是 Windows 还是 Linux 和判断是终端还是 Gvim >>
" =============================================================================

" -----------------------------------------------------------------------------
"  < 判断操作系统是否是 Windows 还是 Linux >
" -----------------------------------------------------------------------------
let g:iswindows = 0
let g:islinux = 0
if(has("win32") || has("win64") || has("win95") || has("win16"))
    let g:iswindows = 1
else
    let g:islinux = 1
endif

" -----------------------------------------------------------------------------
"  < 判断是终端还是 Gvim >
" -----------------------------------------------------------------------------
if has("gui_running")
    let g:isGUI = 1
else
    let g:isGUI = 0
endif

" -----------------------------------------------------------------------------
"  < 编码配置 >
" -----------------------------------------------------------------------------
" 注：使用utf-8格式后，软件与程序源码、文件路径不能有中文，否则报错
set encoding=utf-8                                    "设置gvim内部编码
set fileencoding=utf-8                                "设置当前文件编码
set fileencodings=ucs-bom,utf-8,gbk,cp936,latin-1     "设置支持打开的文件的编码

" 文件格式，默认 ffs=dos,unix
set fileformat=unix                                   "设置新文件的<EOL>格式
set fileformats=unix,dos,mac                          "给出文件的<EOL>格式类型

if (g:iswindows && g:isGUI)
    "解决菜单乱码
    source $VIMRUNTIME/delmenu.vim
    source $VIMRUNTIME/menu.vim

    "解决consle输出乱码
    language messages zh_CN.utf-8
endif

" -----------------------------------------------------------------------------
"  < 界面配置 >
" -----------------------------------------------------------------------------
set number                                            "显示行号
set laststatus=2                                      "启用状态栏信息
set cmdheight=2                                       "设置命令行的高度为2，默认为1
set cursorline                                        "突出显示当前行
" set guifont=YaHei_Consolas_Hybrid:h10               "设置字体:字号（字体名称空格用下划线代替）
set guifont=DejaVu_Sans_Mono:h14			          "self.需要将相关字体安装到c:\Windows\Fonts，已执行
set nowrap                                            "设置不自动换行
set shortmess=atI                                     "去掉欢迎界面
set gcr=a:block-blinkon0                              "禁止光标闪烁
" 设置 gVim 窗口初始位置及大小
if g:isGUI
    " au GUIEnter * simalt ~x                           "窗口启动时自动最大化
    winpos 100 10                                     "指定窗口出现的位置，坐标原点在屏幕左上角
    set lines=38 columns=120                          "指定窗口大小，lines为高度，columns为宽度
endif

" 设置代码配色方案
if g:isGUI
"    colorscheme Tomorrow-Night-Eighties               "Gvim配色方案
     " colorscheme molikai				"self.本配色方案生效之后，界面颜色由原先的白底黑字，变成“gvim便携版”的暗蓝底
     colorscheme solarized				"self.需要安装相应的插件或文件,目录：$VIMRUNTIME\vimfiles\bundle\vim-powerline\autoload\Powerline\Colorschemes 和 $VIMRUNTIME\vimfiles\bundle\vim-powerline\autoload\Powerline\Themes, $VIMRUNTIME\vim74\colors
else
    colorscheme Tomorrow-Night-Eighties               "终端配色方案
endif

" 显示/隐藏菜单栏、工具栏、滚动条，可用 Ctrl + F11 切换
if g:isGUI
    set guioptions-=m
    set guioptions-=T
    set guioptions-=r
    set guioptions-=L
    map <silent> <c-F11> :if &guioptions =~# 'm' <Bar>
        \set guioptions-=m <Bar>
        \set guioptions-=T <Bar>
        \set guioptions-=r <Bar>
        \set guioptions-=L <Bar>
    \else <Bar>
        \set guioptions+=m <Bar>
        \set guioptions+=T <Bar>
        \set guioptions+=r <Bar>
        \set guioptions+=L <Bar>
    \endif<CR>
endif
" -----------------------------------------------------------------------------

"============================备选+低频条目========================================

" 自动补全配置
" 让Vim的补全菜单行为与一般IDE一致(参考VimTip1228)
set completeopt=longest,menu

" Ctrl + i 退出插入模式
imap <M-k> <ESC>

"短横线键-,先删除一行，再粘贴,效果相当于<与下面一行交换位置>
"noremap - ddp

"在normal模式下将光标所在单词大写
"nnoremap <c-u> viwU<esc>

"set ai                                                "self:?? 使得VI文本编辑器能够自动对齐(缩进？)

"
"------------------ 绑定Python解释器，一键F6编译、F5运行---------------------------------
"  需要注意的是，必须将Python加入到系统环境变量中去，不然无法执行的
" self: 部分代码暂时不懂
autocmd BufRead *.py set makeprg=python\ -c\ \"import\ py_compile,sys;\ sys.stderr=sys.stdout;\ py_compile.compile(r'%')\"
autocmd BufRead *.py set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m

"----self.add:添加 -i 参数 实现<交互式编译运行快捷键>
autocmd BufRead *.py nmap <F7> :!ptpython -i %<CR>
autocmd BufRead *.py nmap <F5> :!ipython -i %<CR>
autocmd BufRead *.py nmap <F6> :!python -i %<CR>

"注掉原有F6、F5编译运行，因为已经自定义self<交互式编译运行快捷键>
"autocmd BufRead *.py nmap <F5> :!python  %<CR>
"autocmd BufRead *.py nmap <F6> :make<CR>
"autocmd BufRead *.py copen          "如果是py文件，则同时打开编译信息窗口

" -----------------------------------------------------------------------------
"  < 编写文件时的配置 >
" -----------------------------------------------------------------------------
filetype on                                           "启用文件类型侦测
filetype plugin on                                    "针对不同的文件类型加载对应的插件
filetype plugin indent on                             "启用缩进
set smartindent                                       "启用智能对齐方式
set expandtab                                         "将Tab键转换为空格
set tabstop=4                                         "设置Tab键的宽度
set shiftwidth=4                                      "换行时自动缩进4个空格
set smarttab                                          "指定按一次backspace就删除shiftwidth宽度的空格
set foldenable                                        "启用折叠
set foldmethod=indent                                 "indent 折叠方式
"set foldmethod=marker                                "marker 折叠方式

" 当文件在外部被修改，自动更新该文件
set autoread

" 常规模式下输入 cS 清除行尾空格
nmap cS :%s/\s\+$//g<CR>:noh<CR>

" 常规模式下输入 cM 清除行尾 ^M 符号
nmap cM :%s/\r$//g<CR>:noh<CR>

set ignorecase                                        "搜索模式里忽略大小写
"-----------------------------------------------------------------------------

"插入模式下的<上下左右>键的映射，已经有AutoHotKey的脚本实现，脚本用CapsLk+HJKL实现<上下左右>键的映射
"   下面设定已经失去意义,所以注掉下面设定
        "Ctrl + K 插入模式下光标向上移动
        "imap <c-k> <Up>
        " Ctrl + J 插入模式下光标向下移动
        "imap <c-j> <Down>
        " Ctrl + H 插入模式下光标向左移动
        "imap <c-h> <Left>
        " Ctrl + L 插入模式下光标向右移动
        "imap <c-l> <Right>

"---------------------Alt是windows菜单的常用快捷键,故放弃下面快捷键组合------------------------------------------------
        " Alt + K 插入模式下光标向上移动,并退出插入模式
        "imap <M-k> <Up><ESC>
        " Alt + J 插入模式下光标向下移动,并退出插入模式
        "imap <M-j> <Down><ESC>
        " Alt + H 插入模式下光标向左移动,并退出插入模式
        "imap <M-h> <Left><ESC>
        " Alt + L 插入模式下光标向右移动,并退出插入模式
        "imap <M-l> <Right><ESC>

"=========================self===============self=======================================

"----------------------废弃条目--------------------------------------------------

"self.经测，会出现某某函数已经存在的错误
" vimrc文件修改之后自动加载, windows
"autocmd! bufwritepost _vimrc source %
" vimrc文件修改之后自动加载, linux
"autocmd! bufwritepost .vimrc source %

"在cmd模式下将光标所在单词大写
"inoremap <c-u> <esc>viwU<esc>i     "因<c-u>用作<Undo>,本条放弃
"insert模式下Undo
"inoremap <M-u> <esc>ui     "因为<M-u>是pycharm菜单快捷键,本条放弃

    "尝试将默认shell更换为powershell
    "   remark:可能会出现错误，参见:http://stackoverflow.com/questions/94382/vim-with-powershell
    "   下面代码实现了powershell，但是每次gvim按w保持时，都会报一些buffer错误
    "if has("win32")
    "    set shell=cmd.exe
    "    set shellcmdflag=/c\ powershell.exe\ -NoLogo\ -NoProfile\ -NonInteractive\ -ExecutionPolicy\ RemoteSigned
    "    "下面这条语句的问题是，"d:/python_learn/powershell.exe.lnk\"并没有继承cmd.exe的环境变量，初始目录为".lnk"默认的目录
    "    "set shellcmdflag=/c\ \"d:/python_learn/powershell.exe.lnk\" -NoLogo\ -NoProfile\ -NonInteractive\ -ExecutionPolicy\ RemoteSigned
    "    set shellpipe=|
    "   set shellredir=>
    "endif

"if has("win32")
"    set shell=d:\python_learn\powershell.exe
""    set shellcmdflag=/c\ powershell.exe\ -NoLogo\ -NoProfile\ -NonInteractive\ -ExecutionPolicy\ RemoteSigned
""    set shellcmdflag=/c\ d:/python_learn/powershell.exe.lnk -NoLogo\ -NoProfile\ -NonInteractive\ -ExecutionPolicy\ RemoteSigned
"    set shellpipe=|
"    set shellredir=>
"endif
 
"----------------------废弃条目--------------------------------------------------

"---------------------------self.退出插入模式.思考----------------------------------------------
"       由于<ESC>按键太远，用其退出插入模式不方便，于是可以用下面的设定，兼有退出
"       插入模式的功能。有网友设置的是  Ctrl + i ,我认为也很顺手。现在的问题是，
"       Alt + h 是win系统下的常用快捷键，所以最好避开这个BUG。
"       self:?? self.bug.mark：下面的语句写在最开始的前20行内，设置无效
"       vimrc 文件内的语句就和C语言一样，语句顺序可能影响执行效果



" 分屏窗口移动, Smart way to move between windows
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l


" 命令行模式增强，ctrl - a到行首， -e 到行尾
"cnoremap <C-j> <t_kd>
"cnoremap <C-k> <t_ku>
"cnoremap <C-a> <Home>
"cnoremap <C-e> <End>


" nerdcommenter {{{
let g:NERDSpaceDelims=1
let g:NERDAltDelims_python = 1
" }}}


" -----------------------------------------------------------------------------
"  < neocomplcache 插件配置 >
" -----------------------------------------------------------------------------
" 关键字补全、文件路径补全、tag补全等等，各种，非常好用，速度超快。
" let g:neocomplcache_enable_at_startup = 1     "vim 启动时启用插件
" let g:neocomplcache_disable_auto_complete = 1 "不自动弹出补全列表
" 在弹出补全列表后用 <c-p> 或 <c-n> 进行上下选择效果比较好
"self.not work,要求Lua，本编译版是Lua/dyn
" -----------------------------------------------------------------------------

" --------------------------------20160808---------------------------------------------



" -----------------------------------------------------------------------------
"  Pydiction,python代码补全插件
" -----------------------------------------------------------------------------
" 官方链接：http://www.vim.org/scripts/script.php?script_id=850
" 	    http://rkulla.github.io/pydiction/
" 安装方法： 
"	1.将pyflakes.vim解压到vimfiles\ftplugin目录下 
"	2. 在vimfiles\ftplugin 目录了新建pydiction目录，将complete-dict 和pydiction.py放到pydiction目录下
"" filetype plugin on "用于自动判别文件类型，其他地方已经设定过了，为避免重复设定，注释掉本句

let g:pydiction_location = 'C:\Vim\vimfiles\ftplugin\pydiction\complete-dict'

let g:pydiciton_menu_height = 20

" -----------------------------------------------------------------------------
"  vimtweak,插件，可以实现窗口半透明
" -----------------------------------------------------------------------------
" 官方链接：http://www.vim.org/scripts/script.php?script_id=687
" 安装：直接将解压的 vimtweak.dll 放到gvim.exe同一目录下
" solarize 的配色方案，不适宜应用透明度设置，看起来效果极差。可以研究其他配色方案下的透明度设置

"Alpha Window
call libcallnr("vimtweak.dll","SetAlpha",180)
au GUIEnter * call libcallnr("vimtweak.dll", "SetAlpha", 230)  			"定义了默认的透明度


"定义几个快捷键，M-1将gvim最大化，M-0取消最大化，M-5设置透明度为255，即完全不透明 
map M-2 <Esc>:call libcallnr("vimtweak.dll", "SetAlpha", 200)
map M-3 <Esc>:call libcallnr("vimtweak.dll", "SetAlpha", 230)
map M-5 <Esc>:call libcallnr("vimtweak.dll", "SetAlpha", 255)
map M-8 <Esc>:call libcallnr("vimtweak.dll", "SetAlpha", 180)

"Maximized Windows:

map M-1 <Esc>:call libcallnr("vimtweak.dll", "EnableMaximize", 1)
map M-0 <Esc>:call libcallnr("vimtweak.dll", "EnableMaximize", 0)


"-----------------self-------------------self---------------------------------



"-----------------self-copy.便携版----------------------------------------self-copy.便携版--------------------------------
"-----------------self-copy.便携版----------------------------------------self-copy.便携版--------------------------------
"-----------------self-copy.便携版----------------------------------------self-copy.便携版--------------------------------




" -----------------------------------------------------------------------------
"  < Windows Gvim 默认配置> 做了一点修改
" -----------------------------------------------------------------------------
if (g:iswindows && g:isGUI)
    source $VIMRUNTIME/vimrc_example.vim
    source $VIMRUNTIME/mswin.vim
    behave mswin
    set diffexpr=MyDiff()

    function MyDiff()
        let opt = '-a --binary '
        if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
        if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
        let arg1 = v:fname_in
        if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
        let arg2 = v:fname_new
        if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
        let arg3 = v:fname_out
        if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
        let eq = ''
        if $VIMRUNTIME =~ ' '
            if &sh =~ '\<cmd'
                let cmd = '""' . $VIMRUNTIME . '\diff"'
                let eq = '"'
            else
                let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
            endif
        else
            let cmd = $VIMRUNTIME . '\diff'
        endif
        silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
    endfunction
endif

" -----------------------------------------------------------------------------
"  < Linux Gvim/Vim 默认配置> 做了一点修改
" -----------------------------------------------------------------------------
if g:islinux
    set hlsearch        "高亮搜索
    set incsearch       "在输入要搜索的文字时，实时匹配

    " Uncomment the following to have Vim jump to the last position when
    " reopening a file
    if has("autocmd")
        au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
    endif

    if g:isGUI
        " Source a global configuration file if available
        if filereadable("/etc/vim/gvimrc.local")
            source /etc/vim/gvimrc.local
        endif
    else
        " This line should not be removed as it ensures that various options are
        " properly set to work with the Vim-related packages available in Debian.
        runtime! debian.vim

        " Vim5 and later versions support syntax highlighting. Uncommenting the next
        " line enables syntax highlighting by default.
        if has("syntax")
            syntax on
        endif

        set mouse=a                    " 在任何模式下启用鼠标
        set t_Co=256                   " 在终端启用256色
        set backspace=2                " 设置退格键可用

        " Source a global configuration file if available
        if filereadable("/etc/vim/vimrc.local")
            source /etc/vim/vimrc.local
        endif
    endif
endif


" -----------------------------------------------------------------------------
" -----------------------------------------------------------------------------
"  < Vundle 插件管理工具配置 >
" -----------------------------------------------------------------------------
" 用于更方便的管理vim插件，具体用法参考 :h vundle 帮助
" 安装方法为在终端输入如下命令
" git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
" 如果想在 windows 安装就必需先安装 "git for window"，可查阅网上资料


if g:islinux
    set rtp+=~/.vim/bundle/vundle/
    call vundle#rc()
else
    set rtp+=$VIM/vimfiles/bundle/vundle/
    call vundle#rc('$VIM/vimfiles/bundle/')
endif

" 使用Vundle来管理Vundle，这个必须要有。
Bundle 'gmarik/vundle'

" 以下为要安装或更新的插件，不同仓库都有（具体书写规范请参考帮助）
Bundle 'a.vim'
Bundle 'Align'
Bundle 'jiangmiao/auto-pairs'
Bundle 'bufexplorer.zip'
Bundle 'ccvext.vim'
Bundle 'Yggdroot/indentLine'
Bundle 'Mark--Karkat'
Bundle 'Shougo/neocomplete'
Bundle 'scrooloose/nerdcommenter'
Bundle 'scrooloose/nerdtree'
Bundle 'OmniCppComplete'
Bundle 'Lokaltog/vim-powerline'
Bundle 'msanders/snipmate.vim'
Bundle 'wesleyche/SrcExpl'
Bundle 'std_c.zip'
Bundle 'tpope/vim-surround'
Bundle 'scrooloose/syntastic'
Bundle 'majutsushi/tagbar'
Bundle 'ZoomWin'
Bundle 'tpope/vim-markdown'
"Plugin 'Valloric/YouCompleteMe'
Plugin 'shawncplus/phpcomplete.vim'
" Bundle 'ctrlpvim/ctrlp.vim'
" Bundle 'cSyntaxAfter'
" Bundle 'javacomplete'
" Bundle 'vim-javacompleteex'               "更好的 Java 补全插件
" Bundle 'mattn/emmet-vim'
" Bundle 'fholgado/minibufexpl.vim'         "好像与 Vundle 插件有一些冲突
" Bundle 'Shougo/neocomplcache.vim'
" Bundle 'repeat.vim'
" Bundle 'ervandew/supertab'                "有时与 snipmate 插件冲突
" Bundle 'taglist.vim'
" Bundle 'TxtBrowser'
" Bundle 'Valloric/YouCompleteMe'
" Plugin 'exvim/ex-minibufexpl'                "exvim插件之一,self:??修复BUG
" -----------------------------------------------------------------------------



"-----------------self-copy.便携版----------------------------------------self-copy.便携版--------------------------------
"-----------------self-copy.便携版----------------------------------------self-copy.便携版--------------------------------
"-----------------self-copy.便携版----------------------------------------self-copy.便携版--------------------------------


" ==============================================================================================
"                          << 以下为软件默认配置,用一对双引号加一个空格注释后保留 >>
"                           ----因上面有相同代码
" =========================================================================================================
"" set nocompatible
"" source $VIMRUNTIME/vimrc_example.vim
"" source $VIMRUNTIME/mswin.vim
"" behave mswin
"" set diffexpr=MyDiff()
"" function MyDiff()
""   let opt = '-a --binary '
""   if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
""   if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
""   let arg1 = v:fname_in
""   if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
""   let arg2 = v:fname_new
""   if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
""   let arg3 = v:fname_out
""　 if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
""   let eq = ''
""   if $VIMRUNTIME =~ ' '
""     if &sh =~ '\<cmd'
""       let cmd = '""' . $VIMRUNTIME . '\diff"'
""       let eq = '"'
""     else
""       let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
""     endif
""   else
""     let cmd = $VIMRUNTIME . '\diff'
""   endif
""   silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
"" endfunction


" -----------------------------------------------------------------------------
